
#include "MCU_include.h"

const u8 OffCurrent_Tab[3] = {40, 20, 12/*7,12*/};
 vu8 OffCurrent_Cnt = 0;
    	vu8 OffCurrent_TH = 0;	
    	
    	
    	
void PHASE_Capture_Init2(unsigned char TMC_Clock)
{
	_tmcaf  = 0; //clr TMC TMCPAF
	_tmcon  = 0;
	_tmcc0  &= 0b00001111;
	_tmcc0 |= TMC_Clock; //Set TMC clock
	_tmcm1 = 0; 
	_tmcm0 = 0; 
}


   	
void Check_Pan_Moveout_Current()
{			  										
			if ((B_FLAG_PotDet) &&(FG_IH_Status.flagBit.PWM_ON_OFF == 1 ))
			{
			if(u16_PowerMea >= 2400)
			{
					OffCurrent_TH = OffCurrent_Tab[0];
			}
			else	if(u16_PowerMea > 800)
			{
					OffCurrent_TH = OffCurrent_Tab[1];					
			}
			else	
			{					
					OffCurrent_TH = OffCurrent_Tab[2];	
			}		
	                	

	            if (ADC_I_result < OffCurrent_TH) // C_AD_MinCurr=20
	            {
	                OffCurrent_Cnt++;
	                
	                if (OffCurrent_Cnt > 2) // 2*10ms=20ms
	                {
	                    OffCurrent_Cnt = 0;
	                  	I_PanStatus = 0;
	                  	#ifdef UART_ASCII
	                  	uart_print_uint(ADC_I_result);
	                  	uart_put_char('G');
	                  	#endif
	                }
	            }
	            else
	            {
	            	if(OffCurrent_Cnt >0 )
	                OffCurrent_Cnt--;
	                I_PanStatus = 1;
	            }
            		
            	/**************************************************/          		
			 	/**************************************************/
					if (I_PanStatus ==0)	
					{				

							FG_IH_Status.flagBit.PWM_ON_OFF = 0;	
							IH_Status_APP.flagBit.NoPan = 1;
							FLAG.flagBit.PanMovOut = 1;
							IGBT_OFF(PWM_F);
							PWM_F = d_PWM_F;
							BUZZER_ON_200MS();
							PotMoveOut();										
					}

				
			}
}
//*****************************************************//
//****************************************//
void  Check_Pan_Moveout_Phase()
{
static	vu8 CheckPanPhaseNUM;
		vu8 CheckPanPhase;
	if((ControlSet_App.flagBit.on_off) && (FG_IH_Status.flagBit.PWM_ON_OFF)&&(B_FLAG_PotDet ==1 )/*&&( FLAG.flagBit.PanMovOut  ==0)*/)
	{

//**********************************************************//	
			PhaseNum = Phase_Capture();			//GET PHASE WIDTH	
			PhaseAngle[0] = Time2Angle(PhaseNum);		//GET ANGLE

//**********************************************************//				
		//if(u16_PowerMea >= 2400)
		if(PWM_F <22000)
		{
			CheckPanPhase = CheckPanPhaseMax_2 ;			
		}
		else
		{
			CheckPanPhase = CheckPanPhaseMax_1 ;
		}				
				
		if ((PhaseAngle[0] >= CheckPanPhaseMin) && (PhaseAngle[0] <= CheckPanPhase)) 
		{
			POWER_CTRL.flagBit.FrePhase =1;	
			CheckPanPhaseNUM=0;
		}
		else
		{
			CheckPanPhaseNUM++;
			if(CheckPanPhaseNUM>2)
			{
				CheckPanPhaseNUM=0;
				PotMoveOut();
			//	IH_Status_APP.flagBit.NoPan = 1;			
			//	FLAG.flagBit.PanMovOut  = 1;			
			//	FG_IH_Status.flagBit.PWM_ON_OFF = 0;		
//				Lim_Frequ = MIN_FRE;
//				PWMMAX_Update(Lim_Frequ);			
				FLAG.flagBit.PanMovOutCheck = 0;
				#ifdef UART_ASCII	
				uart_print_uint(	PhaseAngle[0]);	
				uart_put_char('D');
				#endif
			}
		}
		_phasee	=1;
		
	}

}
//****************************************//
//name :Check_Pan_Movein
//input:
//output:
//******************************************************//
//******************************************************// 
void Check_Pan_Movein_Phase()	
{				 
	static 	vu8 Phase_Angle =0;		
			   if((FG_IH_Status.flagBit.Machine_ON_OFF == 1)&& (FLAG.flagBit.PanMovOut == 1 ) \
			    &&( IH_Status_APP.flagBit.error == 0)&& (u16_PowerSet!=0))				
				{			 
						
					MoveInCnt++;			
					bit_BUZZER = ON;
					BUZZER_ON_20MS();
					_phasee = 0;
			/*		_mss0 = 0;
					_mss1 = 0;*/
					Phase_Angle = PotCheck();
										
					if (Phase_Angle <= CheckPanPhaseMax_1 && Phase_Angle >CheckPanPhaseMin )		//50
					{
					
						PotStatus = 1;			////There is a pot	
					}
					else 
					{
					
						PotStatus =  0;			//No pot		
					}
					_phasef = 0;
					_phasee = 1;
							
					
					if (PotStatus!=0)
					{												
							MoveInCnt = 0;
							PotStatus = 0;
							PotExist();
							B_FLAG_PotDet =0;	
																	
					}
					else
					{								
							PotMoveOut();				
					}						

				}				
		//**************************************************//	
}


/*********************
name: ()
function 
input: 
output: 
*********************************************/
unsigned char PotCheck()
{	
volatile unsigned int Phase_Capture_Num, Temp_Phase_Angle;
	static u8 ia;
	static  u16 PHASE_CAPTURE_Min, PHASE_CAPTURE_Max;
	static u16 PHASE_SUM;
	PWM_F_Back = PWM_F;
	_pwmon = 0; 

		_pwmpl = 1100 & 0xff;	//set pwm frequency 30KHz    Fpwm/(pwmp[11:0]+1)  = 32Mhz/1066  =30Khz	
		_pwmph = 1100 >> 8;
		_pwmdl = 550 & 0xff;	//			(
		_pwmdh = 550 >> 8;		
	
	_pwmpf=0;
	_pwmdf=0;	
	_mss0 = 0;
	_mss1 = 0;
	_emi =0;	
	_pwmon = 1;
	while(!_pwmpf);
	_pwmpf = 0;
	while(!_pwmpf);
	_pwmpf = 0;
	while(!_pwmpf);
	_pwmpf = 0;
	while(!_pwmpf);
	_pwmpf = 0;		

	for(ia=0;ia<10;ia++)
	{	
		Phase_Capture_Num 	= 	Phase_Capture();
		if(ia == 0)
		{
			PHASE_CAPTURE_Max = Phase_Capture_Num ;
			PHASE_CAPTURE_Min = Phase_Capture_Num;
		}
		else
		{
			if(Phase_Capture_Num > PHASE_CAPTURE_Max)
			PHASE_CAPTURE_Max	 =Phase_Capture_Num ;
			if(Phase_Capture_Num < PHASE_CAPTURE_Min)	
			PHASE_CAPTURE_Min	=Phase_Capture_Num;
		}
		PHASE_SUM +=Phase_Capture_Num;
	}	
	_pwmon = 0; 
	_emi =1;
	PHASE_SUM -= PHASE_CAPTURE_Max;
	PHASE_SUM -= PHASE_CAPTURE_Min;
	Phase_Capture_Num =	PHASE_SUM/8 ;
	Temp_Phase_Angle 	= 	Time2Angle(Phase_Capture_Num);			
	PWM_Freq_Init(1,PWM_F_Back);
	PHASE_SUM =0;
//	
				#ifdef UART_ASCII
				uart_print_uint(Phase_Capture_Num);	
				uart_put_char('Q');	
				uart_print_uint(Temp_Phase_Angle);	
				uart_put_char('D');
				#endif
			
	return 	Temp_Phase_Angle ;
}



void PotMoveOut()
{
	IH_Status_APP.flagBit.NoPan = 1;
	FLAG.flagBit.PanMovOut  = 1;
	FG_IH_Status.flagBit.PWM_ON_OFF = 0;
	FLAG.flagBit.PowerONFlag = 0;
	IGBT_OFF(PWM_F);
	PWM_F = d_PWM_F;
//	B_FLAG_PotDet = 0;	
	FG_IH_Status.flagBit.PWOER_SET_CHANGE =1;
	POWER_CTRL.flagBit.Power_OK =0;
		
}


void PotExist()
{
	PWM_F = d_PWM_F;
	PWM_Freq_Init(1,d_PWM_F);
	FAN = ON;
	_pwmon=1;
	PWM_ON();
	FLAG.flagBit.PanMovOut = 0;
	IH_Status_APP.flagBit.NoPan = 0;
	FG_IH_Status.flagBit.PWM_ON_OFF = 1;		
	FG_IH_Status.flagBit.Machine_ON_OFF = 1;
	POWER_CTRL.flagBit.Power_OK = 0;
//		FG_IH_Status.flagBit.PWOER_SET_CHANGE =1;
	OVP2_Over_I_flag =0;	
	u16_PowerMeaSum = 0;
	u16_PowerMea = 0;
	PowerMeaCnt = 0;
}
